package com.hongshan.wallet.core.comm

import android.app.Activity
import android.arch.lifecycle.ViewModelProviders
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.Constant
import com.hongshan.wallet.R
import com.hongshan.wallet.base.BaseOnPageChangeListener
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.router.go
import com.hongshan.wallet.util.AutoInject
import kotlinx.android.synthetic.main.activity_guide.*

/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.comm
 * @author   : zhangxx
 * @date     : 2019/4/19
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
@Route(path = RouterIndex.GUIDE)
class GuideActivity:MyActivity<ViewDataBinding,GuideViewModel>() {

    @AutoInject(Constant.ROUTE_PARAMS_KEY.GUIDE_IMAGE)
    var imgRes:ArrayList<Int>? = null
    @AutoInject(Constant.ROUTE_PARAMS_KEY.GUIDE_NEXT_URL)
    var nextUrl :String? = null
    @AutoInject(Constant.ROUTE_PARAMS_KEY.GUIDE_NEED_BUTTON)
    var needButton = false

    override fun initVariableId() = 0

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_guide

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setResult(Activity.RESULT_OK)
        imgRes = imgRes?: arrayListOf()
        if(imgRes?.size == 1){
            tvNext.text = "完成"
        }
        tvNext.visibility = if (needButton) View.VISIBLE else View.GONE
        val onClickListener = View.OnClickListener {
            if (vpGuide.currentItem == imgRes!!.size - 1) {
                finish()
                if (nextUrl != null && nextUrl!!.isNotEmpty()) {
                    go(nextUrl!!)
                }
            } else {
                vpGuide.currentItem++
            }
        }
        vpGuide.adapter = object : PagerAdapter() {
            override fun isViewFromObject(p0: View, p1: Any) = p0 == p1

            override fun getCount() = imgRes!!.size

            override fun instantiateItem(container: ViewGroup, position: Int): Any {
                val view = ImageView(this@GuideActivity)
                view.run {
                    scaleType = ImageView.ScaleType.CENTER_CROP
                    setImageResource(imgRes!![position])
                }
                container.addView(view)
                if (!needButton){
                    view.setOnClickListener(onClickListener)
                }
                return view
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                container.removeView(`object` as View)
            }
        }
        vpGuide.addOnPageChangeListener(BaseOnPageChangeListener{
            if (it == imgRes!!.size - 1) {
                tvNext.text = "完成"
            }else {
                tvNext.text = "下一步"
            }
        })
        tvNext.setOnClickListener(onClickListener)
    }

    override fun initViewModel() = ViewModelProviders.of(this)[GuideViewModel::class.java]

    companion object{
        const val REQUEST_CODE_GUIDE = 10086
    }
}